/**
 * 
 */
package org.vsg.stock.basic.service.business;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.annotation.Resource;

import org.vsg.common.biz.exception.ServiceException;
import org.vsg.stock.basic.domain.BalanceLine;
import org.vsg.stock.basic.domain.BalanceLineCharacter;
import org.vsg.stock.basic.domain.BalanceLineCharacter.BalanceLineCharacterPK;
import org.vsg.stock.basic.persistence.BalanceLineDao;
import org.vsg.stock.core.business.StockDailyKLineBO;
import org.vsg.stock.core.domain.StockDailyKLine;

/**
 * @author Bill Vison
 *
 */
public abstract class OrgDataStockBalanceLineFinderBO extends
		AbstractStockBalanceLineFinderBO {


	
	@Resource
	private StockDailyKLineBO stockDailyKLineBO;	
	
	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.business.AbstractStockBalanceLineFinderBO#scanLines(java.util.Date, int)
	 */
	@Override
	public Collection<BalanceLine> scanLines(Date recentDate, int aheadCycle)
			throws ServiceException {
		// --- the main logic ---
		
		/**
		 * get stock kline list in reference cycle from recent date 
		 */
		stockDailyKLineBO.setStockCode( this.getStockCode() );
		List<StockDailyKLine>  stkKlineList = stockDailyKLineBO.findStockLineByCycle(recentDate, aheadCycle);
		
		List<BalanceLine> balaLines =  scanLines( stkKlineList );
		
		
		List<BalanceLineCharacter> blcList = null;
		// ---- search and merge exist line ----
		BalanceLineCharacter balLineChar = null;
		for (BalanceLine balLine : balaLines) {
			balLineChar = new BalanceLineCharacter();
			
			BalanceLineCharacterPK pk = balLineChar.new BalanceLineCharacterPK();
			pk.setCode( this.getFinderCode() );
			pk.setLineId(  balLine.getLineId() );
			balLineChar.setId( pk );
			
			blcList = balLine.getCharasters();
			if ( blcList == null) {
				blcList = new Vector<BalanceLineCharacter>();
			}
			blcList.add(  balLineChar );
			balLine.setCharasters(blcList);
		}
		
		return balaLines;
	}

	/**
	 * 
	 * @param stkKlineList
	 * @return
	 * @throws ServiceException
	 */
	protected abstract List<BalanceLine> scanLines(List<StockDailyKLine>  stkKlineList) throws ServiceException;
	
	
}
